<?php
 
namespace RedMVC;

/**
 * Page class
 *  
 * @category    RedMVC
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Page{
    
    public $controller;
    public $action;
    public $params = array();
    public $uri;
    public $name;
    public $parent;
    public $isActive = false;
    public $isVisible = true;
    protected $childs = array();
    public $cssClass;
    public $resource;
    public $privilege;
    
    public static function mvc($name, $controller, $action, $params = array(), $childs = array(), $isVisible = true){
        $page               = new self();
        $page->name         = $name;
        $page->controller   = $controller;
        $page->action       = $action;
        $page->params       = $params;
        $page->cssClass     = self::makeCssClass($name);
        $page->resource     = $controller;
        $page->privilege    = $action;
        $page->isVisible    = $isVisible;
        $page->setChilds($childs);
        return $page;
    }
    
    public static function uri($name, $uri, $childs = array(), $isVisible = true){
        $page               = new self();
        $page->name         = $name;
        $page->uri          = $uri;
        $page->cssClass     = self::makeCssClass($name);
        $page->resource     = $page->cssClass;
        $page->privilege    = null;
        $page->isVisible    = $isVisible;
        $page->setChilds($childs);
        return $page;
    }

    public static function active(){
        $request            = Registry::get('RedMVC_Request');
        $page               = new self();
        $page->controller   = $request->getControllerName();
        $page->action       = $request->getActionName();
        return $page;
    }
 
    private static function makeCssClass($name){
        $name = str_replace(
            array('ä','Ä','ö','Ö','ü','Ü','ß', ' '),
            array('ae','Ae','oe','Oe','ue','Ue','ss', '-'),
            $name
        );
        return preg_replace("#[^a-z0-9-_]+#", '', strtolower($name));
    }
    
    public function setChilds(array $childs){
        $this->childs = $childs;
        foreach($this->childs as $child){
            $child->parent = $this;
        }
    }
    
    public function getChilds(){
        return $this->childs;
    }
    
}